package com.thunder.reader.ui;

import com.thunder.reader.R;
import com.thunder.reader.provider.Feed;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddGroupActivity extends Activity {
    private static final String TAG = "AddFeedActivity";
    public static final String EXTRA_GROUP_ID = "extra_group_id";
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.add_group_activity);
        boolean editGroup = false;
        int groupId = -1;
        final Intent intent = getIntent();
        if (intent != null && intent.hasExtra(EXTRA_GROUP_ID)) {
            groupId = (int) intent.getLongExtra(EXTRA_GROUP_ID, -1);
        }
        editGroup = (groupId != -1);
        final Button buttonSave = (Button) findViewById(R.id.add_group_save);
        final Button buttonCancel = (Button) findViewById(R.id.add_group_cancel);
        buttonSave.setEnabled(false);
        final EditText inputDescription = (EditText) findViewById(R.id.add_group_input_description);
        final TextView dialogTitle = (TextView) findViewById(R.id.add_group_title);
        final EditText inputTitle = (EditText) findViewById(R.id.add_group_input_title);
        inputTitle.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                // nothing to do now
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // nothing to do now
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    buttonSave.setEnabled(true);
                } else {
                    buttonSave.setEnabled(false);
                }
            }
        });
        if (editGroup) {
            // load info fill it to edit boxes'
            dialogTitle.setText(R.string.edit_group);
            final Uri sourceUri = ContentUris.withAppendedId(Feed.Group.CONTENT_URI, groupId);
            final Cursor cursor = getContentResolver().query(sourceUri, 
                    new String[]{Feed.Group.TITLE, Feed.Group.DESCRIPTION}, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                inputTitle.setText(cursor.getString(0));
                inputDescription.setText(cursor.getString(1));
            }
            cursor.close();
        } else {
            dialogTitle.setText(R.string.add_group);
        }
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        
        buttonSave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // get all stuff and do enough validation before saving to database
                final String title = inputTitle.getText().toString();
                if (TextUtils.isEmpty(title)) {
                    finish();
                }
                final String description = inputDescription.getText().toString();
                final ContentValues cv = new ContentValues();
                cv.put(Feed.Group.TITLE, title);
                cv.put(Feed.Group.DESCRIPTION, description);
                getContentResolver().insert(Feed.Group.CONTENT_URI, cv);
                finish();
            }
        });
    }
}
